﻿package bipede.animation
{
	import events.MoveEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.*;
	import flash.utils.Timer;
	import bipede.membres.Person;
	
	public class Timeline extends MovieClip
	{
		static private var timeDescription:Vector.<Pose> = new Vector.<Pose>();
		//static private var compteur:Timer;
		static private var currentFrame:int;
		static public var started:Boolean = false;
		
		public function Timeline()
		{
		}
		static private function _tick( event:TimerEvent ):void
		{
			if ( event.currentTarget.currentCount >= timeDescription.length )
			{
				//compteur.stop();
				return;
			}
			SceneManager.show( timeDescription[ event.currentTarget.currentCount ], true );
		}
		static public function record( temps:int, pose:Pose ):void
		{
			timeDescription[ temps ] = pose;
			
			trace( "record : \r" + timeDescription );
		}
		static public function show( temps:int, anim:Boolean = false ):void
		{
			SceneManager.show( timeDescription[ temps ], false );
		}		

		static public function start(event=null):void
		{
			currentFrame = 0;
			
			started = true;
			SceneManager.show( timeDescription[ currentFrame ], true );
			//instance.dispatchEvent( new AnimationEvent( AnimationEvent.START ) );PoseEvent ? SoftBodyEvent ? MoveEvent ?
			
			
			// attente de nextframe plutot que Timer :			
			/*
			if ( compteur == null )
			{
				compteur = new Timer(1000, 10);
				compteur.addEventListener( TimerEvent.TIMER, _tick );
				compteur.addEventListener( TimerEvent.TIMER_COMPLETE, _fin_tick );
			}
			compteur.reset();
			compteur.start(); 
			*/			
		}	
		static public function stop(event=null):void
		{
			started = false;
		}
		static public function pause(event=null):void
		{
			started = !started;
		}
		
		static public function nextframe( event:MoveEvent ):void
		{
			if ( !started ) return;
			
			currentFrame++;
			
			if ( currentFrame >= timeDescription.length )
			{
				stop();
				return;
			}
			
			trace("nextframenextframenextframe " + currentFrame);
			
			var temporisation:Timer = new Timer(20, 1);
			temporisation.addEventListener( TimerEvent.TIMER_COMPLETE, temporisation_nextframe );
			temporisation.start(); 
			
			//SceneManager.show( timeDescription[ currentFrame ], true );
		}
		static private function temporisation_nextframe( event:TimerEvent ):void
		{
			SceneManager.show( timeDescription[ currentFrame ], true );
		}			
	}
}